package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.*;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.DailyTrainCarriageMapper;
import com.jiawa.train.business.req.DailyTrainCarriageQueryReq;
import com.jiawa.train.business.req.DailyTrainCarriageSaveReq;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DailyTrainCarriageService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;
    @Resource
    private TrainCarriageService trainCarriageService;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(DailyTrainCarriageSaveReq req) {
        //将传递的参数赋给DailyTrainCarriage对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        //在保存的时候自动计算座位数和列数
        List seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColumnCount(seatColEnums.size());
        req.setSeatCount(req.getRowCount() * req.getColumnCount());
        DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(req, DailyTrainCarriage.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(dailyTrainCarriage.getId())){
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }
        //否则进行更新操作
        else{
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.updateByPrimaryKeySelective( dailyTrainCarriage);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryDailyTrainCarriageList(DailyTrainCarriageQueryReq  req) {
        //创建dailyTrainCarriageExample对象
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.setOrderByClause("date desc, train_code asc, `index` asc");
        //设置查询条件
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();
        if(ObjectUtil.isNotNull(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        if(ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<DailyTrainCarriage> dailyTrainCarriageList = dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);
        //返回查询结果,转为分页的形式,
        PageInfo<DailyTrainCarriage> pageInfo = new PageInfo<>(dailyTrainCarriageList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainCarriageMapper.deleteByPrimaryKey(id);
    }

    //生成每日车厢信息
    @Transactional
    public void genDailyCarriage(Date date, String trainCode) {
        //1.删除日期为date的某车次的车厢信息
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();
        criteria.andDateEqualTo(date);
        criteria.andTrainCodeEqualTo(trainCode);
        dailyTrainCarriageMapper.deleteByExample(dailyTrainCarriageExample);
        //2.查询该车次所有的火车车厢的信息
        List<TrainCarriage> trainCarriageList = trainCarriageService.selectByTrainCode(trainCode);
        if(CollUtil.isEmpty(trainCarriageList)){
            log.info("没有查询到车厢信息");
            return;
        }
        //3.遍历火车车厢的信息，然后生成每日车厢信息
        for(TrainCarriage TrainCarriage : trainCarriageList){
            DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(TrainCarriage, DailyTrainCarriage.class);
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setDate(date);
            //数据库中的字段名不一致导致的
            dailyTrainCarriage.setColumnCount(TrainCarriage.getColCount());
            dailyTrainCarriage.setCreateTime(new Date());
            dailyTrainCarriage.setUpdateTime(new Date());
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }
    }

    public List<DailyTrainCarriage> selectCarriagesBySeatType(Date date, String trainCode, String seatTypeCode) {
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();
        criteria.andDateEqualTo(date);
        criteria.andTrainCodeEqualTo(trainCode);
        criteria.andSeatTypeEqualTo(seatTypeCode);
        return dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);
    }
}